package com.example.teachingevaluationsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingevaluationsystem.entity.*;
import com.example.teachingevaluationsystem.mapper.EvaluationPlanMapper;
import com.example.teachingevaluationsystem.service.*;
import com.example.teachingevaluationsystem.vo.EvaluationPlanVo;
import com.example.teachingevaluationsystem.vo.PlanSubmitVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 评价计划 服务实现类
 *
 * @author lp
 * @since 2022-04-04
 */
@Service
@AllArgsConstructor
public class EvaluationPlanServiceImpl extends ServiceImpl<EvaluationPlanMapper, EvaluationPlan>
    implements EvaluationPlanService {

  private final PlanTopicService planTopicService;

  private final EvaluationStatusService evaluationStatusService;
  private final StudentEvaluateService studentEvaluateService;

  private final TeacherEvaluationService teacherEvaluationService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean save(EvaluationPlanVo evaluationPlan) {
    final EvaluationPlan plan = new EvaluationPlan();
    BeanUtils.copyProperties(evaluationPlan, plan);
    saveOrUpdate(plan);
    planTopicService.remove(
        new LambdaQueryWrapper<PlanTopic>().eq(PlanTopic::getPlanId, plan.getId()));
    Long topicId = evaluationPlan.getTopicId();
    final PlanTopic planTopic = new PlanTopic();
    planTopic.setPlanId(plan.getId());
    planTopic.setTopicId(topicId);
    return planTopicService.saveOrUpdate(planTopic);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void save(
      Long userId, List<PlanSubmitVo> submitVoList, Long courseId, Long planId, boolean isTeacher) {
    // 答题状态 保存
    EvaluationStatus evaluationStatus = new EvaluationStatus();
    evaluationStatus.setStatus(1);
    evaluationStatus.setUserId(userId);
    evaluationStatus.setCourseId(courseId);
    evaluationStatus.setPlanId(planId);
    evaluationStatusService.save(evaluationStatus);
    if (isTeacher) {
      final List<TeacherEvaluation> teacherEvaluations =
          submitVoList.stream()
              .map(
                  planSubmitVo -> {
                    final TeacherEvaluation teacherEvaluation = new TeacherEvaluation();
                    teacherEvaluation.setTeacherId(userId);
                    teacherEvaluation.setCourseId(courseId);
                    teacherEvaluation.setPlanId(planId);
                    teacherEvaluation.setTopicId(planSubmitVo.getTopicId());
                    teacherEvaluation.setFraction(planSubmitVo.getFraction());
                    return teacherEvaluation;
                  })
              .toList();
      teacherEvaluationService.saveBatch(teacherEvaluations);
    } else {
      // 答题分数 保存
      List<StudentEvaluate> studentEvaluates =
          submitVoList.stream()
              .map(
                  planSubmitVo -> {
                    StudentEvaluate studentEvaluate = new StudentEvaluate();
                    studentEvaluate.setStudentId(userId);
                    studentEvaluate.setCourseId(courseId);
                    studentEvaluate.setTopicId(planSubmitVo.getTopicId());
                    studentEvaluate.setPlanId(planId);
                    studentEvaluate.setFraction(planSubmitVo.getFraction());
                    return studentEvaluate;
                  })
              .toList();
      studentEvaluateService.saveBatch(studentEvaluates);
    }
  }
}
